package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.SmallVideosService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @author げっしょく
 * @date 2021/11/1
 */
@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {

    @Autowired
    private SmallVideosService smallVideosService;

    /**
     * 发布小视屏
     * @param videoThumbnail 视频封面
     * @param videoFile 视频
     * @return
     */
    @PostMapping
    public ResponseEntity saveVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        smallVideosService.saveVideos(videoThumbnail, videoFile);
        return ResponseEntity.ok(null);
    }

    //分页查看视屏
    @GetMapping
    public ResponseEntity getVideos(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = smallVideosService.getVideos(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 关注用户
     * @param id
     * @return
     */
    @PostMapping("/{id}/userFocus")
    public ResponseEntity userFocus(@PathVariable Long id) {
        smallVideosService.Focus(id, true);
        return ResponseEntity.ok(null);
    }

    /**
     * 取消关注
     * @param id
     * @return
     */
    @PostMapping("/{id}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable Long id) {
        smallVideosService.Focus(id, false);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频点赞
     * @param id
     * @return
     */
    @PostMapping("/{id}/like")
    public ResponseEntity like(@PathVariable ObjectId id) {
        smallVideosService.VideoLike(id, true);
        return ResponseEntity.ok(null);
    }


    /**
     * 视频取消点赞
     * @param id
     * @return
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable ObjectId id) {
        smallVideosService.VideoLike(id, false);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频评论列表
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity comments(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize,
                                   @PathVariable ObjectId id) {
        PageResult pageResult = smallVideosService.getComments(page, pagesize, id);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 发布评论
     * @param id
     * @return
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity saveComment(@PathVariable ObjectId id, @RequestBody Map map) {
        String comment = (String) map.get("comment");
        smallVideosService.saveComment(id, comment);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频评论点赞
     * @param id
     * @return
     */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity commentsLike(@PathVariable ObjectId id) {
        smallVideosService.VideoCommentLike(id, true);
        return ResponseEntity.ok(null);
    }


    /**
     * 视屏评论取消点赞
     * @param id
     * @return
     */
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity commentsDisLike(@PathVariable ObjectId id) {
        smallVideosService.VideoCommentLike(id, false);
        return ResponseEntity.ok(null);
    }

}
